package com.chzc.fm.duotin.ui.channel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.RankBookAdapter;
import com.chzc.fm.duotin.bean.channel.Rank;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/7/6.
 */
public class SpecialRankActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView ivback;
    private TextView tvtitle;
    private RecyclerView recyclerView;

    private RankBookAdapter adapter;

    private String dataid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_specialrank);

        initViews();
        initEvents();
        initDatas();

    }

    public void initViews()
    {
        ivback = (ImageView) findViewById(R.id.specialrank_back_iv);
        tvtitle = (TextView) findViewById(R.id.specialrank_title_tv);
        recyclerView = (RecyclerView) findViewById(R.id.specialrank_content_rv);

        LinearLayoutManager manager = new LinearLayoutManager(SpecialRankActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
    }

    public void initEvents()
    {

        ivback.setOnClickListener(this);
    }

    public void initDatas()
    {
        Rank  rank = (Rank) getIntent().getSerializableExtra("specialitem");
        dataid = rank.getItem_value();
        tvtitle.setText(rank.getTitle());

        getSpecialRank(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");
                    if("SUCCESS".equals(error_msg))
                    {
                        JSONObject data = root.getJSONObject("data");
                        JSONArray datalist = data.getJSONArray("content_list");
                        List<Rank> rankList = Rank.arrayRankFromData(datalist.toString());

                        adapter = new RankBookAdapter(SpecialRankActivity.this,rankList);
                        recyclerView.setAdapter(adapter);
                        adapter.setItemOnClickListener(new RankBookAdapter.mItemOnClickListener() {
                            @Override
                            public void OnItemClick(int position) {

                            }
                        });


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    public void getSpecialRank(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {

                return HttpUtil.doGet(GlobalParam.URL_SPECIALRANK_HEAD + dataid + GlobalParam.URL_SPECIALRANK_TAIL);
            }
        };

        new DuotinTask(request,callback).execute();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.specialrank_back_iv:
                finish();
                break;
        }
    }
}
